<template>
  <div>
    <div class="user-hander">
      <h4>分类管理</h4>
      <div>
        <el-button
          type="success"
          size="mini"
          @click="handleAdd"
          icon="el-icon-circle-plus-outline"
        >
          新建分类</el-button
        >
        <el-button size="mini" icon="el-icon-refresh-left" @click="userRefresh"
          >刷新</el-button
        >
      </div>
    </div>
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      border
      style="width: 100%"
    >
      <el-table-column label="ID" prop="id" width="400"> </el-table-column>

      <el-table-column label="分类名" prop="name" width="700">
      </el-table-column>

      <el-table-column align="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-remove-outline"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @current-change="changePage"
      :page-size="10"
      :page-count="11"
      layout="prev, pager, next"
      :total="total"
    >
    </el-pagination>

    <!-- 新建分类对话框 -->
    <el-dialog
      title="新建分类"
      :visible.sync="addCategoryVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="addForm" label-width="80px">
        <el-form-item
          label="分类名称"
          :rules="[{ type: 'String', message: '请输入字符串类型' }]"
        >
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit(addForm)">新建</el-button>
          <el-button @click="addCategoryVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 分类修改对话框 -->
    <el-dialog
      title="修改分类"
      :visible.sync="editCategoryVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="editForm" :model="editForm" label-width="80px">
        <el-form-item label="分类ID" class="spanHiden">
          <p>{{ editForm.id }}</p>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onEdit(editForm)">确定</el-button>
          <el-button @click="editFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 删除分类对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Axios from "axios";
export default {
  data() {
    return {
      //分类数据list
      tableData: [],
      //表头查询关键字
      search: "",
      //页码
      page: 1,
      //数据总数
      total: 20,
      // 新增表单数据
      addForm: {
        name: "",
      },
      // 修改表单数据
      editForm: {
        id: "1",
        name: "",
      },
      //弹出框 Boolean
      addCategoryVisible: false,
      editCategoryVisible: false,
      centerDialogVisible: false,
    };
  },
  created() {
    this.getCategoryList();
  },
  methods: {
    //新增分类
    handleAdd() {
      this.addCategoryVisible = true;
    },
    //编辑行
    handleEdit(index, row) {
      this.editCategoryVisible = true;
      console.log("点击编辑数据行为", index, row);
      this.selectCategoryById(row.id);
    },
    //删除行
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("是否删除该分类数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delCategoryById(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //新增分类表单提交
    onSubmit(addForm) {
      console.log(addForm);
      Axios({
        url: "http://localhost:8080/category/",
        method: "post",
        data: {
          name: addForm.name,
        },
      })
        .then((res) => {
          // console.log(res)
          if (res.data.code == 1) {
            this.$message({
              type: "success",
              message: "创建分类成功",
            });
          } else {
            this.$message.error({
              message: res.data.msg,
            });
            return;
          }
          this.addForm = [];
          this.addCategoryVisible = false;
          this.getCategoryList();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //修改分类表单提交
    onEdit(editForm) {
      console.log("修改分类表单数据为", editForm);
      Axios({
        url: "http://localhost:8080/category/",
        method: "put",
        data: {
          id: editForm.id,
          name: editForm.name,
        },
      })
        .then((res) => {
          console.log(res);
          if (res.data.code == 1) {
            this.$message({
              type: "success",
              message: "修改分类成功",
            });
          } else {
            this.$message.error({
              message: res.data.msg,
            });
            return;
          }
          this.editForm = [];
          this.editCategoryVisible = false;
          this.getCategoryList();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //加载页面
    openFullScreen() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
      });
      setTimeout(() => {
        loading.close();
      }, 1000);
    },
    //刷新页面
    userRefresh() {
      this.search = "";
      this.getCategoryList();
    },
    // 请求分类信息
    getCategoryList() {
      this.openFullScreen();
      Axios.get("http://localhost:8080/category/" + this.page + "/10")
        .then((res) => {
          // console.log(res.data.data.list);
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //删除分类请求
    delCategoryById(row) {
      Axios.delete("http://localhost:8080/category/" + row.id)
        .then((res) => {
          // console.log(res)
          if (res.data.code == 0) {
            this.$message.error({
              message: res.data.msg,
            });
            return;
          }
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getCategoryList();
        })
        .catch((err) => {
          this.$message.error({
            message: "删除失败",
          });
          console.log(err);
        });
    },
    //分页点击时间
    changePage(e) {
      console.log("切换到页数" + e);
      this.page = e;
      this.getCategoryList();
    },
    // 获取指定id
    selectCategoryById(id) {
      Axios.get("http://localhost:8080/category/" + id)
        .then((res) => {
          console.log("获取指定id", res.data.data);
          this.editForm = res.data.data;
        })
        .catch((err) => {
          this.$message.error({
            message: err,
          });
        });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
};
</script>

<style>
.spanHiden {
  display: none;
  overflow: hidden;
}
.user-hander {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
.el-pagination {
  text-align: left;
  background-color: white;
}
</style>